<?php

namespace interface\pda;

/*
*个人中心接口类
*/

class myAppClass extends index
{
	/*个人中心首页*/
	public function home()
	{
		$this->code         = 1;
		$data['result']     = [
			'name'       => $this->control['name'] ?? '',
			'ico'        => self::fillSrc($this->control['ico'] ?? '', $this->control['updateTime'] ?? ''),
			'job'        => $this->controlJob['name'] ?? '',
			'department' => $this->controlDepartment['name'] ?? '',
			'company'    => para('wechatTitle'),
			'hotline'    => para('hotline'),
			'userName'   => self::$user['userName'] ?? '',
		];
		$this->resp['data'] = $data;
	}
	
	/**
	 * 通知设置
	 */
	public function setting()
	{
		$source = $this->header['Source'];
		$data   = [];
		if ($source == 'oa') {
			$user_sertting = query('user_setting', "stid = '" . self::$stid . "' and type = 'OA通知设置'");
			$data          = json_decode($user_sertting['value'], true);
		}
		$this->code         = 1;
		$this->resp['data'] = $data;
	}
	
	/**
	 * OA消息设置
	 */
	public function oaMessageSetting()
	{
		$isOpen = $this->post('isOpen');
		$info   = query('user_setting', "stid = '" . self::$stid . "' and type = 'OA通知设置'");
		if (empty($info['id'])) {
			$this->resp['warn'] = '未找到此设置';
		} else {
			$setting            = json_decode($info['value'], true);
			$setting['message'] = $isOpen;
			$warn               = static::update(array('table' => 'user_setting', 'field' => ['value' => json_encode($setting, 320)], 'where' => "id = '$info[id]'"));
			if ($warn == '更新成功') {
				$this->code         = 1;
				$this->resp['warn'] = '设置成功';
			} else {
				$this->resp['warn'] = '设置失败';
			}
		}
	}
	
	/**
	 * 语言切换
	 */
	public function langSetting()
	{
		$lang = $this->post('lang') ?: 'SC';
		if (!$lang) {
			// 参数错误
			$this->resp['warn'] = L('homepage.languageToggle.langErr');
		} else {
			$where = [
				'type' => '系统语言',
				'stid' => static::$stid,
			];
			$info  = query('user_setting', generateWhere($where));
			if ($info['id']) {
				$json['warn'] = static::update([
					'table' => 'user_setting',
					'field' => ['value' => $lang],
					'where' => generateWhere($where),
				]);
			} else {
				$json['warn'] = static::insert([
					'table' => 'user_setting',
					'field' => array_merge($where, ['value' => $lang]),
				]);
			}
			if (!in_array($json['warn'], ['更新成功', '新增成功'])) {
				$this->resp['warn'] = L('homepage.languageToggle.langErr');
			} else {
				$_SESSION['lang']   = $lang;
				$this->resp['warn'] = L('homepage.languageToggle.langSuss');
				$this->code         = 1;
			}
		}
	}
	
	/**
	 * 关于
	 */
	public function about()
	{
		$this->code = 1;
		$type       = $this->post('type');
		$content    = match ($type) {
			'用户服务及隐私协议' => query('document', "title = '用户服务及隐私协议' and state = '已发布' order by time desc"),
			default              => query('document', "title = '公司介绍' and state = '已发布' order by time desc"),
		};
		
		$data['result']     = [
			'text' => html_entity_decode($content['content'], ENT_QUOTES),
		];
		$this->resp['data'] = $data;
	}
	
	/**
	 * 更新日志
	 */
	public function updateLog()
	{
		$this->code     = 1;
		$data['result'] = [];
		$type           = match ($this->post('miniProgram')) {
			'crm', 'oa', 'erp', 'pda' => $this->post('miniProgram'),
			default                   => '其他',
		};
		$all            = find([
			'table' => 'document',
			'where' => "title = '更新日志' and dcid in (select id from document_catalog where name = '{$type}小程序端') and state = '已发布' order by time desc",
		]);
		foreach ($all['array'] as $val) {
			$data['result'][] = ['text' => html_entity_decode($val['content'], ENT_QUOTES)];
		}
		$this->resp['data'] = $data;
	}
	
	/**
	 * 退出登录
	 *
	 * @return void
	 */
	public function loginOut()
	{
		$token = empty($this->header['Auth']) ? false : $this->header['Auth'];
		//更新时间
		if ($token) {
			$this->rSet($token, null);
			$this->code         = 1;
			$this->resp['warn'] = '退出成功';
		}
	}
	
}
